package com.example.jmeter.enums;

import com.example.jmeter.dependent.Command;

/**
 * @program: spring-agent
 * @description:
 * @author: zt22328
 * @create: 2023-04-20 11:16
 **/
public enum JmeterCommand{

    VERSION("-v"),

    /**
     * jmeter -n -t baidu_requests_results.jmx -r -l baidu_requests_results.jtl -e -o /home/tester/apache-jmeter-3.0/resultReport
     * jmeter -Jthreads=200 -n -t 'D:\tool\jmx\gen\openApi\A04.jmx' -l log04.jtl -e -o D:\tool\jmx\gen\A04
     */
    STANDALONE_TEST_PLAN("-n -t %s -l %s -e -o %s"),

    /**
     * 集群远程调用
     *
     * jmeter -n -R 192.168.1.200,192.168.1.201 -t test.jmx -l test.jtl -e -o D:\gitlab\jmeter_script\panguoqing\report
     */
    CLUSTER_TEST_PLAN("-n -R %s -t %s -l %s -e -o %s"),
    ;


    private String command;

    JmeterCommand(String command) {
        this.command = command;
    }

    public String getCommand() {
        return command;
    }

    public String getCommand(String jmx, String jtl, String destPath) {
        return getCommand(jmx, jtl, destPath, null, null);
    }

    public String getCommand(String jmx, String jtl, String destPath, Integer threads, Integer loops) {
        if (threads == null && loops == null) {
            return String.format(JMETER_EXECUTION_1, jmx, jtl, destPath);
        }else if (threads != null && loops != null) {
            return String.format(JMETER_EXECUTION_2, threads, loops, jmx, jtl, destPath);
        }else if (threads != null && loops == null) {
            return String.format(JMETER_EXECUTION_3, threads, jmx, jtl, destPath);
        }
        return String.format(JMETER_EXECUTION_4, loops, jmx, jtl, destPath);
    }


    public static final String JMETER_PARAM = "${__P(%s, %s)}";
    public static final String JMETER_EXECUTION_1 = "-n -t %s -l %s -e -o %s";
    public static final String JMETER_EXECUTION_2 = "-Jthreads=%d -Jloops=%d -n -t %s -l %s -e -o %s";
    public static final String JMETER_EXECUTION_3 = "-Jthreads=%d -n -t %s -l %s -e -o %s";
    public static final String JMETER_EXECUTION_4 = "-Jloops=%d -n -t %s -l %s -e -o %s";

    public static final String getJmeterParam(String flied, String value) {
        return String.format(JMETER_PARAM, flied, value);
    }
}
